package com.atguigu.bigdata.spark.core.rdd.operator.transform;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function;
import scala.Tuple2;

import java.util.Arrays;
import java.util.List;

//根据单词首字母进行分组
public class Spark06_RDD_Operator_Transform1_JAVA {
    public static void main(String[] args) {
        SparkConf conf = new SparkConf().setMaster("local[*]").setAppName("sparkCore");
        JavaSparkContext sc = new JavaSparkContext(conf);
        List<String> list = Arrays.asList("Hello", "Spark", "Scala", "Hadoop");
        JavaRDD<String> rdd = sc.parallelize(list);

        JavaPairRDD<String,Iterable<String>> groupby = rdd.groupBy(new Function<String, String>() {
            @Override
            public String call(String s) throws Exception {
                return String.valueOf(s.charAt(0));
            }
        });

        List<Tuple2<String, Iterable<String>>> res = groupby.collect();
        for(Tuple2 val : res) {
            System.out.println(val.toString());
        }

        sc.stop();
    }
}
